package online.westonline.com.westonline.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.nekocode.badge.BadgeDrawable;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.entry.IamgeBean;
import online.westonline.com.westonline.entry.NodeBean;
import online.westonline.com.westonline.utils.GsonUtils;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 另一种形式的list适配器
 * Created by xiatao on 2018/6/15.
 */

public class NodeListAdpter extends RecyclerView.Adapter<NodeListAdpter.NodeListViewHolder> {
    List<NodeBean> datas = new ArrayList<>();
    private boolean selected=false;
    private static final int MYLIVE_MODE_CHECK = 0;
    int mEditMode = MYLIVE_MODE_CHECK;

    private longclicktoopen longclicktoopen;
    private clickto clickto;
    public void setDatas(List<NodeBean> datas) {
        this.datas = datas;
    }

    public List<NodeBean> getDatas() {
        return datas;
    }

    public void setClickto(NodeListAdpter.clickto clickto) {
        this.clickto = clickto;
    }

    public void setLongclicktoopen(NodeListAdpter.longclicktoopen longclicktoopen) {
        this.longclicktoopen = longclicktoopen;
    }


    @NonNull
    @Override
    public NodeListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.node_normallist_item, parent, false);
        return new NodeListViewHolder(view);
    }

    public void setEditMode(int editMode) {
        mEditMode = editMode;
        notifyDataSetChanged();
    }
    @Override
    public void onBindViewHolder(@NonNull NodeListViewHolder holder, final int position) {
        final NodeBean bean = datas.get(position);
        //选择框的配置
        if (mEditMode == MYLIVE_MODE_CHECK)
        {
            holder.checkboxTodeleteList.setVisibility(View.GONE);
        }
        else
        {
            holder.checkboxTodeleteList.setVisibility(View.VISIBLE);
        }
        if (bean.getCheck())
        {
            holder.checkboxTodeleteList.setChecked(true);
        }
        else
        {
            holder.checkboxTodeleteList.setChecked(false);
        }
        holder.tvNodeListContent.setText(bean.getContent());
        if (TextUtils.isEmpty(bean.getTitle())) {
            holder.tvNodeListTitle.setText("无标题");
        } else {
            holder.tvNodeListTitle.setText(bean.getTitle());
        }
        holder.tvNodeListTime.setText(bean.getTime());
        if (!TextUtils.isEmpty(bean.getImg_paths())) {
            IamgeBean bean1 = GsonUtils.parseJSON(bean.getImg_paths(), IamgeBean.class);
            if (bean1.getPaths().size() > 0) {
                Glide.with(holder.itemView.getContext()).load(new File(bean1.getPaths().get(0))).into(holder.imgNodeListPre);
                BadgeDrawable drawable = new BadgeDrawable.Builder()
                        .type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
                        .text1(bean1.getPaths().size() + "张")
                        .badgeColor(UiUtlis.getColor(R.color.TopicItemNodeColor))
                        .textSize(15)
                        .build();
                holder.imgNodeListCount.setBackground(drawable);
            }
        }
        //点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickto!=null)
                {
                    clickto.click(position,datas);
                }
            }
        });
        //长按事件
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longclicktoopen != null)
                {
                    longclicktoopen.open(position,datas);
                }
                return true;
            }
        });


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class NodeListViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.checkbox_todelete_list)
        CheckBox checkboxTodeleteList;
        @BindView(R.id.tv_node_list_title)
        TextView tvNodeListTitle;
        @BindView(R.id.tv_node_list_content)
        TextView tvNodeListContent;
        @BindView(R.id.tv_node_list_time)
        TextView tvNodeListTime;
        @BindView(R.id.img_node_list_pre)
        ImageView imgNodeListPre;
        @BindView(R.id.img_node_list_count)
        ImageView imgNodeListCount;


        public NodeListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    public interface longclicktoopen
    {
        void open(int pos, List<NodeBean> datas);
    }
    public interface clickto
    {
        void click(int pos, List<NodeBean> datas);
    }
}
